package com.supreme.simpleserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.commonmybatis.mapper.GoodsMapper;
import com.supreme.commonmybatis.entity.primary.MBGoods;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.simpleserver.service.GoodsService;
import com.supreme.simpleserver.service.mybatis.MBGoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "goods")
public class GoodsController {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsService goodsService;

    @Resource
    private MBGoodsService mbGoodsService;

    @GetMapping(value = "search")
    public AjaxResp searchGoods() {
        List<MBGoods> goodsList = goodsMapper.selectList(Wrappers.emptyWrapper());
        return new AjaxResp(goodsList);
    }

    @GetMapping(value = "findOne")
    public AjaxResp findOne(@RequestParam(value = "goodsId") Long goodsId) {
        MBGoods goods = this.goodsService.findOneSvc(goodsId);
        return new AjaxResp(goods);
    }

    @GetMapping(value = "findOneSleep")
    public AjaxResp findOneSleep(@RequestParam(value = "goodsId") Long goodsId) {
        MBGoods goods = this.goodsService.findOneSleepSvc(goodsId);
        return new AjaxResp(goods);
    }

    @PostMapping(value = "create")
    public AjaxResp createGoods(@RequestBody MBGoods goods) {
        boolean save = this.mbGoodsService.save(goods);
        return new AjaxResp(save);
    }

    @PutMapping(value = "update/{goodsId}")
    public AjaxResp updateGoods(@PathVariable(value = "goodsId") Long goodsId, @RequestBody MBGoods goods) {
        return new AjaxResp(null);
    }

    @PutMapping(value = "deductGoodsStock/{goodsId}")
    public AjaxResp deductGoodsStock(@PathVariable(value = "goodsId") Long goodsId, @RequestBody JSONObject postData) {
        Integer deductStock = postData.getInteger("deductStock");
        Long sleepSecond = postData.getLong("sleepSecond");
        boolean updateResult = this.goodsService.deductGoodsStockSvc(goodsId, deductStock, sleepSecond);
        return new AjaxResp(updateResult);
    }

    @PutMapping(value = "deductGoodsStock2/{goodsId}")
    public AjaxResp deductGoodsStock2(@PathVariable(value = "goodsId") Long goodsId, @RequestBody JSONObject postData) {
        Integer deductStock = postData.getInteger("deductStock");
        Long sleepSecond = postData.getLong("sleepSecond");
        boolean updateResult = this.goodsService.deductGoodsStockSvc(goodsId, deductStock, sleepSecond);
        return new AjaxResp(updateResult);
    }

    @PutMapping(value = "deductGoodsStockAmount/{goodsId}")
    public AjaxResp deductGoodsStockAmount(@PathVariable(value = "goodsId") Long goodsId, @RequestBody JSONObject postData) {
        Integer deductStock = postData.getInteger("deductStock");
        int result = this.goodsService.deductGoodsStockAmount(goodsId, deductStock);
        return new AjaxResp(result);
    }
}
